#!/bin/bash
#
# Functions for manipulating text files, including extracting values from
# simple name=value files and splitting files into sections.
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 19 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


## divide_file <input file> [head line] [tail line]
##
## Divides a file into pieces, using single sentinel lines in the file as
## the division point. These sentinel lines must not be blank lines, and it
## is strongly recommended that they not contain special characters. Slashes
## (/) are forbidden.
##
## If both the [head line] and [tail line] are omitted, this function simply
## behaves like cat(1). If only a [head line] is supplied, the part of the
## file after the first instance of [head line] is printed (not including the
## matching [head line] string itself). To omit the [head line] but use a
## [tail line], pass an explicit empty string "" as the [head line] argument.
##
## If only a non-empty [tail line] is supplied, this function prints that
## portion of the <input file> starting at the beginning and running up to,
## but not including, the [tail line]. If both [head line] and [tail line]
## are supplied, prints the section of the file between [head line] and
## [tail line], not including the matched lines.
##
## Returns 0 if the match is successful and prints the result. Returns
## non-zero if the match did not succeed, and does not produce output in such
## a case.
##
function divide_file() {
	local -i begin=1
	local -i end=-1
	local status=0
	local chk=
	local data=
	
	if [[ -n "$2" ]]; then
		chk=$(awk "/^$2$/ { print NR + 1; exit 0; }" "$1")
		if [[ -n "${chk}" ]]; then
			begin=${chk}
		else
			status=1
		fi
	fi
	
	if ((status == 0)); then
		if [[ -n "$3" ]]; then
			chk=$(awk "/^$3$/ { print NR - 1; exit 0; }" "$1")
			if [[ -n "${chk}" ]]; then
				end=${chk}
			else
				status=1
			fi
		fi
	fi
	
	if ((status == 0)); then
		data=$(cat "$1")
		
		if ((begin > 0)); then
			data=$(echo "${data}" | tail -n +${begin})
		fi
		
		if ((end > 0)); then
			# Account for the fact that we already stripped off the first part
			end=$((end - begin + 1))
			data=$(echo "${data}" | head -n ${end})
		fi
		
		if [[ -n "${data}" ]]; then
			echo "${data}"
		fi
	fi
	
	return ${status}
}


## file_value <filename> <key>
##
## Parses simplistic configuration files that have key=value directives, one
## per line. Echoes the value of the first matching <key>. Returns 0 if the key
## is found, non-zero if the key cannot be found or if <filename> cannot be
## opened.
##
function file_value() {
	local data=
	local status=0
	
	data=$(grep -m 1 "^$2=" "$1")
	status=$?
	
	if ((status == 0)); then
		echo "${data}" | awk -F = '{print $2}'
		status=$?
	fi
	
	return ${status}
}
